package org.xjc.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.xjc.annotation.Log;
import org.xjc.utils.DateUtils;
import org.xjc.utils.IdGenerator;
import org.xjc.entity.OperationLogEntity;
import org.xjc.mapper.OperationLogMapper;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLogEntity> implements OperationLogService {

    @Resource
    private OperationLogMapper operationLogMapper;

    @Override
    public void addLog(Class<?> clazz, Method method, Log log, Object returnData, long executeTime, Map<String, Object> paramData, String servicePersonId) {
        Date now = new Date();
        OperationLogEntity operationLogEntity = new OperationLogEntity();
        operationLogEntity.setId(IdGenerator.nextId());
        operationLogEntity.setGmtCreate(new Date());
        operationLogEntity.setOperationDate(DateUtils.format(now, DateUtils.TimeFormat.DATE_L));
        operationLogEntity.setOperationTime(DateUtils.format(now, DateUtils.TimeFormat.TIME_C));
        operationLogEntity.setDescription(log.value());
        operationLogEntity.setExecuteTime(String.valueOf(executeTime));
        operationLogEntity.setOperationUserId(servicePersonId);
        if (null != paramData) {
            operationLogEntity.setParams(paramData.toString());
        }
        if (null != returnData) {
            operationLogEntity.setReturnData(returnData.toString());
        }
        operationLogMapper.insert(operationLogEntity);
    }

}